package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Cc
 * @version 1.0
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    private DtsAdminMapper dtsAdminMapper;


    /**
     * 根据用户查询一个对象
     * @param username
     * @return
     */
    @Override
    public List<DtsAdmin> findAdmin(String username) {
        QueryWrapper<DtsAdmin> wrapper =new QueryWrapper<>();
        wrapper.eq("username",username);
        //删除逻辑,查询都要加上
        wrapper.eq("deleted",false);
        List<DtsAdmin> dtsAdmins = dtsAdminMapper.selectList(wrapper);
        return dtsAdmins;
    }

    /**
     *   分页查询 后台用户
     * @param username
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<DtsAdmin> findListByQuery(String username, String sort, String order, Integer page, Integer limit) {
        return dtsAdminMapper.selectPage(new Page(page, limit)
                ,new QueryWrapper<DtsAdmin>()
                        .eq(!StringUtils.isEmpty(username),"username",username)
                        .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort)
        );
    }

    /**
     * 添加管理员方法
     * @param admin
     * @return
     */
    @Override
    public DtsAdmin create(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        //..
        admin.setPassword(new BCryptPasswordEncoder().encode(admin.getPassword()));
        dtsAdminMapper.insert(admin);
        return admin;
    }

    @Override
    public int updateById(DtsAdmin admin) {
        return dtsAdminMapper.updateById(admin);
    }

    @Override
    public void deleteById(Integer anotherAdminId) {
        dtsAdminMapper.deleteById(anotherAdminId);
    }


}
